//  该文件是Count组件的Reducer,专门为Count组件提供服务，本身即为函数
/**
 * conunt组件的Reducer
 *
 * @param preState  上一次保存的状态，初始化时时undefined
 * @param action 操作 {type:'xx',data:'数据，可选'}
 * @returns 返回处理后的一个新的state
 */

import { INCREMENT, DECREMENT } from '../contant/count';
import { anyAction } from './interface';

export interface PreState {
    count: number;
}
export interface Action {
    type: string;
    payload: anyAction;
}

const preState: PreState = {
    count: 0
}; // 初始化count

const Count = (state: PreState = preState, action: Action) => {
    console.log('xxxxx1222', action, state);
    const { type, payload } = action;
    switch (type) {
        case INCREMENT:
            return { ...state, count: state.count - Number(payload) };
        case DECREMENT:
            return { ...state, count: state.count + Number(payload) };
        default:
            return preState;
    }
};

export default Count;
